﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerBienLaiTraChiTietDao : IBienLaiTraChiTietDao
    {
        public SqlServerBienLaiTraChiTietDao()
        {
            SQLUtility.TableName = "BienLaiTraChiTiet";
        }

        //Trả về đối tượng báo ăn
        private BienLaiTraChiTiet MakeBienLaiTraChiTiet(DataRow row)
        {
            Guid _id = Guid.Parse(row["DetailTraID"].ToString());
            Guid _bienLaiTraID = Guid.Parse(row["BienLaiTraID"].ToString());
            Guid _danhMucThuTraID = Guid.Parse(row["DanhMucThuTraID"].ToString());
            decimal _dongia = decimal.Parse(row["Dongia"].ToString());
            decimal _soluong = decimal.Parse(row["Soluong"].ToString());
            decimal _thanhtien = decimal.Parse(row["Thanhtien"].ToString());

         return new BienLaiTraChiTiet(_id, _bienLaiTraID, _danhMucThuTraID, _dongia, _soluong, _thanhtien);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<BienLaiTraChiTiet> MakeMakeBienLaiTraChiTiets(DataTable dt) 
        {
            IList<BienLaiTraChiTiet> list = new List<BienLaiTraChiTiet>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeBienLaiTraChiTiet(row));
            }
            return list;
        }
        public IList<Business_Objects.BienLaiTraChiTiet> GetBienLaiTraChiTiet()
        {
            SQLUtility.TableName = "BienLaiThu";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeMakeBienLaiTraChiTiets(dt);
        }

        public Business_Objects.BienLaiTraChiTiet GetBienLaiTraChiTietByID(string BienLaiTraChiTietID)
        {
            SQLUtility.TableName = "BienLaiThu";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@BaoAnID", BienLaiTraChiTietID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeBienLaiTraChiTiet(row);
            }
        }

        public void InsertBienLaiTraChiTiet(Business_Objects.BienLaiTraChiTiet BienLaiTraChiTiet)
        {
            SQLUtility.TableName = "BienLaiThu";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(BienLaiTraChiTiet), true);
        }

        public int UpdateBienLaiTraChiTiet(Business_Objects.BienLaiTraChiTiet BienLaiTraChiTiet)
        {
            SQLUtility.TableName = "BienLaiThu";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(BienLaiTraChiTiet)); }
            catch { return -1; }
        }

        public int DeleteBienLaiTraChiTiet(Business_Objects.BienLaiTraChiTiet BienLaiTraChiTiet)
        {
            SQLUtility.TableName = "BienLaiThu";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(BienLaiTraChiTiet)); }
            catch { return -1; }
        }

        private DbParameter[] Take(BienLaiTraChiTiet BienLaiTraChiTiet)
        {
            DbParameter[] parameters = new DbParameter[6];
            parameters[0] = new SqlParameter("@DetailTraID", BienLaiTraChiTiet.Id);
            parameters[1] = new SqlParameter("@BienLaiTraID", BienLaiTraChiTiet.BienLaiTraID);
            parameters[2] = new SqlParameter("@DanhMucThuTraID", BienLaiTraChiTiet.DanhMucThuTraID);
            parameters[3] = new SqlParameter("@Dongia", BienLaiTraChiTiet.Dongia);
            parameters[4] = new SqlParameter("@Soluong", BienLaiTraChiTiet.Soluong);
            parameters[5] = new SqlParameter("@Thanhtien", BienLaiTraChiTiet.Thanhtien);
            return parameters;
        }


        public List<BienLaiTraChiTiet> GetBienLaiTraChiTietByBienLaiTraID(string BienLaiTraID) 
        {
            SQLUtility.TableName = "BienLaiTraChiTiet";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@BienLaiThuID", BienLaiTraID);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectBienLaiTraChiTietsByAndBienLaiTraID", parameters);
            if (dt == null) { return null; }
            else
            {

                return MakeMakeBienLaiTraChiTiets(dt).ToList();  

            }
        }
    }
}
